// pages/detail/detail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_copy: 1
  },

  //复制函数
  copyText: function (e) {
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text + '',
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var itemId = options.itemId; //点击商品后，传过来一个itemId显示该商品的详细信息
    var user_id = wx.getStorageSync('user_id');
    that.onLoadDetail(itemId); //加载详细信息
    that.onAddVisitHistory(user_id, itemId); //添加访问记录
    that.onAddVisitors(itemId); //添加访问人数
  },

  //添加浏览人数+1
  onAddVisitors: function(item_id) {
    var that = this;
    wx.request({
      url: app.globalData.targetUrl + 'items/counter',
      data: {
        item_id: item_id
      },
      success: res => {
        console.log(res.data);
      }
    })
  },



  //访问item接口,根据item_id拿到物品详细信息
  onLoadDetail: function(itemId) {
    var that = this;
    wx.request({
      url: app.globalData.targetUrl + 'items/' + itemId + '/',
      success: res => {
        var item = res.data.data;
        var image_urls = item.image_urls;
        var thumb_nails = [];
        for (var i = 0; i < image_urls.length; i++) {
          thumb_nails[i] = image_urls[i].slice(0, image_urls[i].length - 4) + "_tb.jpg";
        }
        var showQQ = item.qq + '';
        var is_copy = 1;
        if(showQQ.length < 5){
          showQQ = "暂未填写";
          is_copy = 0;
        }else{
          showQQ = showQQ[0] + showQQ[1] + '***' + showQQ[showQQ.length - 2] + showQQ[showQQ.length - 1];
        }
        that.onGetUserOpenid(item.user_id);
        that.setData({
          item: item,
          is_copy: is_copy,
          showQQ: showQQ,
          view_cnt: parseInt(item.view_cnt) + 1,
          thumb_nails: thumb_nails
        })
      }
    })
  },

  onGetUserOpenid: function(user_id) {
    var that = this;
    wx.request({
      url: app.globalData.targetUrl + 'wx/openid/',
      data: {
        user_id: user_id,
        is_wxapp: 0
      },
      success: res => {
        if (res.data.error == 0) {
          that.setData({
            openid: res.data.data.openid,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  //添加访问记录
  onAddVisitHistory(user_id, item_id) {
    var that = this;
    wx.request({
      url: app.globalData.targetUrl + 'history/item/',
      method: "POST",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data: {
        user_id: user_id,
        item_id: item_id,
        is_publish: 0
      },
      success: res => {
        console.log(res.data);
      }
    })
  },
  onChatTap: function(e) {
    var to_user_id = e.detail.target.dataset.user_id;
    var from_user_id = wx.getStorageSync("user_id");
    if (from_user_id == to_user_id) {
      wx.showModal({
        showCancel: false,
        title: '啊哦',
        content: '不能给自己发信息哦',
      })
      var user_id = from_user_id
      var formId = e.detail.formId;
      app.saveFormId(user_id, formId);
    } else {
      wx.navigateTo({
        url: '/pages/chatPage/chatPage?from_user_id=' + from_user_id + '&to_user_id=' + to_user_id,
      });
      var user_id = from_user_id;
      var formId = e.detail.formId;
      app.saveFormId(user_id, formId);
    }
  },
  //点击图片放大 保存
  previewImage: function(e) {
    var that = this;
    var list = [];
    for (var index in that.data.item.image_urls) {
      list[index] = that.data.item.image_urls[index]
    }
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接 
      urls: list // 需要预览的图片http链接列表 
    })
  }

})